
package com.scs.application.modules.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.upms.entity.LoginLog;
import com.scs.application.modules.upms.mapper.LoginLogMapper;
import com.scs.application.modules.upms.request.LoginLogQueryRequest;
import com.scs.application.modules.upms.service.LoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* 登录日志 service 实现
*/
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class LoginLogServiceImpl extends BaseServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {

    @Override
    public List<LoginLog> list(LoginLogQueryRequest request) {
        List<LoginLog> list = this.list(getQueryWrapper(request));
        return list;
    }

    @Override
    public IPage<LoginLog> page(LoginLogQueryRequest request) {
        IPage page = this.page(request.getPage(), getQueryWrapper(request));
        return page;
    }

    private QueryWrapper<LoginLog> getQueryWrapper(LoginLogQueryRequest request){
        return Wrappers.<LoginLog>query()
                .eq(StringUtils.isNotBlank(request.getLoginKey()), "login_key", request.getLoginKey())
                .between(request.getBeginLoginTime() != null && request.getEndLoginTime() != null, "login_time", request.getBeginLoginTime(), request.getEndLoginTime())
                .eq(StringUtils.isNotBlank(request.getFlagSuccess()), "flag_success", request.getFlagSuccess())
            .orderByDesc("login_time");
    }
}
